package com.akcome.apollo.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.akcome.apollo.domain.VStatsPoolDay;
import com.akcome.apollo.dto.PoolLuckyResult;
import com.akcome.apollo.dto.Poolblocklucky;
import com.akcome.apollo.repository.VStatsPoolDayRepository;
import com.akcome.apollo.service.VStatsPoolDayService;


/**
 * VIEWService接口实现
 *
 * @author wn
 * @version $v: 1.0.0, $time:2017-12-26 08:49:00 Exp $
 */
@Service
public class VStatsPoolDayServiceImpl implements VStatsPoolDayService {

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private VStatsPoolDayRepository vStatsPoolDayRepository;
	
	@Override
	public VStatsPoolDay save(VStatsPoolDay vStatsPoolDay){
		return vStatsPoolDayRepository.save(vStatsPoolDay);
	}
	
	@Override
	public VStatsPoolDay findOne(Long id){
		return vStatsPoolDayRepository.findOne(id);
	}
	
	@Override
	public List<VStatsPoolDay> findByDayBetween(String beginDate,String endDate,String cointype){
		return vStatsPoolDayRepository.findByDayBetween(beginDate, endDate,cointype);
	}
	
	@Override
	public PoolLuckyResult findPoolLuckyResult(String cointype) throws Exception{
		//查询近90天数据
		Date endDate = new Date();
		Date beginDate = DateUtils.addDays(endDate, -90);
		SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
		List<VStatsPoolDay> vStatsPoolDays = findByDayBetween(sf.format(beginDate), sf.format(endDate),cointype);
		List<Poolblocklucky> poolblockluckys = new ArrayList<>();
		if(CollectionUtils.isNotEmpty(vStatsPoolDays)){
			for (VStatsPoolDay  poolDay : vStatsPoolDays) {
				Poolblocklucky poolblocklucky = new Poolblocklucky();
				poolblocklucky.setDate(sf.parse(""+poolDay.getDay()).getTime());
				poolblocklucky.setValue(poolDay.getLucky());
				poolblockluckys.add(poolblocklucky);
			}
		}
		PoolLuckyResult result = new PoolLuckyResult();
		result.setPoolblocklucky(poolblockluckys);
		return result;
	}
}
